import Axios from "axios";
import QS from "qs";
import router from "../router";

Axios.defaults.timeout = 120000;
Axios.defaults.withCredentials = true;
// Axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8';
// Axios.defaults.headers.Authorization = 'Bearer ' + window.sessionStorage.token;

// 请求拦截器
Axios.interceptors.request.use(
  (config) => {
    config.headers.Authorization = "Bearer " + window.sessionStorage.token;
    config.headers.post["Content-Type"] =
      "application/x-www-form-urlencoded;charset=UTF-8";
    return config;
  },
  (error) => {
    return Promise.error(error);
  }
);
//响应拦截器
Axios.interceptors.response.use(
  (res) => {
    if (res.data.code === 200) {
      return Promise.resolve(res.data);
    } else {
      switch (res.data.code) {
        case 401:
          // console.log(router);
          console.log(res.data.msg);
          break;
        case 403:
          console.warning("登录过期");
          sessionStorage.removeItem("token");
          setTimeout(() => {
            router.replace({
              path: "/login",
              query: {
                redirect: router.currentRoute.fullPath,
              },
            });
          }, 1000);
          break;
        case 404:
          console.error("网络请求不存在");

          break;
      }
      return Promise.resolve(res.data);
    }
    // if (res.data.code == 403) {
    //   //token失效
    //   let url = null;
    //   if (top != window) {
    //     try {
    //       url = top.location.href;
    //       console.log(1, url);
    //     } catch (e) {
    //       url = document.referrer;
    //       console.log(2, url);
    //     }
    //   }
    //   console.log(url);
    //   // window.open(url,"_parent")
    //   top.location.href = url.split("#")[0];
    // } else {
    //   return res;
    // }
  },
  (error) => {
    console.error("服务器错误");
    return Promise.error(error);
  }
);

const $http = {
  get: function (url, params) {
    return new Promise((resolve, reject) => {
      Axios.get(url, {
        params: params,
      })
        .then((res) => {
          resolve(res.data);
        })
        .catch((err) => {
          reject(err.data);
        });
    });
  },
  getBlob: function (url, params) {
    return new Promise((resolve, reject) => {
      Axios.get(url, {
        params: params,
        responseType: "blob",
      })
        .then((res) => {
          resolve(res.data);
        })
        .catch((err) => {
          reject(err.data);
        });
    });
  },
  post: function (url, params) {
    return new Promise((resolve, reject) => {
      Axios.post(url, QS.stringify(params))
        .then((res) => {
          resolve(res.data);
        })
        .catch((err) => {
          reject(err.data);
        });
    });
  },

  postUpload: function (url, params) {
    return new Promise((resolve, reject) => {
      Axios({
        method: "post",
        url: url,
        headers: { "Content-Type": "multipart/form-data" },
        data: params,
      })
        .then((res) => {
          resolve(res.data);
        })
        .catch((err) => {
          reject(err);
        });
    });
  },
  postBody: function (url, params) {
    return new Promise((resolve, reject) => {
      Axios({
        method: "post",
        url: url,
        headers: { "Content-Type": "application/json;chartset=utf-8" },
        data: params,
      })
        .then((res) => {
          resolve(res.data);
        })
        .catch((err) => {
          reject(err);
        });
    });
  },

  // put: function (url, params) {
  //     return new Promise((resolve, reject) => {
  //         Axios.put(url, params).then(res => {
  //             resolve(res.data);
  //         }).catch(err => {
  //             reject(err.data)
  //         })
  //     })
  // },
  putForm: function (url, params) {
    return new Promise((resolve, reject) => {
      Axios({
        method: "put",
        url: url,
        headers: {
          "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8",
        },
        params: params,
      })
        .then((res) => {
          resolve(res.data);
        })
        .catch((err) => {
          reject(err);
        });
    });
  },
  putBody: function (url, params) {
    return new Promise((resolve, reject) => {
      Axios({
        method: "put",
        url: url,
        headers: { "Content-Type": "application/json;chartset=utf-8" },
        data: params,
      })
        .then((res) => {
          resolve(res.data);
        })
        .catch((err) => {
          reject(err);
        });
    });
  },
  delete: function (url, params) {
    return new Promise((resolve, reject) => {
      Axios.delete(url, {
        params,
      })
        .then((res) => {
          resolve(res.data);
        })
        .catch((err) => {
          reject(err.data);
        });
    });
  },

  all: function (resArrFun) {
    return new Promise((resolve, reject) => {
      Axios.all(resArrFun)
        .then(
          Axios.spread(function () {
            resolve(arguments);
          })
        )
        .catch((err) => {
          reject(err);
        });
    });
  },
};

export { $http };
